<%
  game = Game.find(session[:player].game_id)
  game_setting = game.game_setting
  game_error = GameError.find(:first, :conditions=>["game_id=? AND year=?", game.id, game.current_year])
  player = Player.find(session[:player].id)
  b_players = Player.find(:all, :conditions=>["game_id=? AND player_type=?", game.id, "B"])
  ars = Account.find(:all, :conditions=>["game_id=? AND creditor_id=? AND trade_year=?", game.id, player.id, game.current_year])
  pb = PlayerBalance.find(:first, :conditions=>["player_id=? AND year=?", player.id, game.current_year])
%>
<a href="/user/plan_game">วางแผนการผลิตและการเงิน (A)</a> &nbsp;&nbsp;&nbsp;&nbsp;
<a href="/user/rawmarket">ตลาดซื้อขาย <%=game_setting.inventory_a_name%></a>
<br/>
<%if game.a_run_flag=='Y'%>
  <font color="green">คุณสามารถส่งคำสั่งได้</font>
<%else%>
  <font color="red">คุณไม่สามารถส่งคำสั่งได้</font>
<%end%>
<br/>
<center>
<span style="font-size:30px; color:red;"><u><%=@word%></u></span>
</center>
<br/>
Game Session : <%=game.name%> &nbsp;&nbsp;&nbsp;&nbsp; Year: <%=game.current_year%> &nbsp;&nbsp;&nbsp;&nbsp; Round: -
&nbsp;&nbsp;&nbsp;&nbsp;
<%if game.a_run_flag=='N'%>
<img src="/images/stop_icon.jpg" width="50px" style="vertical-align:middle;"/>
<%else%>
<img src="/images/start_icon.gif" width="50px" style="vertical-align:middle;"/>
<%end%>
&nbsp;&nbsp;&nbsp;&nbsp;
<span style="font-size:30px;">Market Size:</span>
<span style="font-size:40px; color:blue;">? <%#=number_with_delimiter(game.market_size)%></span>
<br/>
<table width="100%">
<tr align="center" valign="top">
  <td width="45%">
    <h3>Offer Order</h3>
    <form action="/user/place_offer" method="post">
    <fieldset>
      <table>
        <tr align="left">
          <td>Inventory(หน่วย):</td>
          <td colspan="3"><%=number_with_delimiter(pb.inventory_unit - pb.sold_unit)%></td>
        </tr>
        <tr align="left">
          <td>Price(บาท):</td>
          <td>=<input type="text" name="price" size="10"/></td>
          <td>Credit(ปี):</td>
          <td>
            =<select name="credit">
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
            </select>
          </td>
        </tr>
        <tr align="left">
          <td>Quantity(ล้านหน่วย):<br/></td>
          <td>=<input type="text" name="quantity" size="10" value="0.00"/></td>
          <td colspan="2"><input type="submit" value="Offer"/></td>
        </tr>
        <%if game.game_setting.direct_trade=='Y'%>
        <tr>
          <td>Offer To:</td>
          <td>
            <select name="direct_trade">
              <option value="0">Market</option>
              <%b_players.each do |b|%>
              <option value="<%=b.id%>"><%=b.name%></option>
              <%end%>
            </select>
          </td>
          <td></td>
        </tr>
        <%end%>
      </table>
    </fieldset>
    </form>
    <%if game_setting.allow_put_option_a=='Y'%>
    <h3>Exercise Option</h3>
    <form action="/user/use_put_option" method="post">
    <fieldset>
      <table width="100%">
        <tr>
          <td>Available(หน่วย): </td>
          <td><%=number_with_delimiter(pb.put_option_remain_unit)%></td>
          <td>ราคาใช้สิทธิ์ :</td>
          <td><%=game_error.put_exprice_a%></td>
        </tr>
        <tr>
          <td>Exercise(ล้านหน่วย):</td>
          <td><input type="text" name="quantity" size="10" value="0.00"/></td>
          <td colspan="2"><input type="submit" value="Submit"/></td>
        </tr>
      </table>
    </fieldset>
    </form>
    <%end%>
    ราคาของผู้ขายรายใหญ่(บาท):<%=game_setting.unit_cost%>, Credit(ปี):0
    <br/>
    <%
      asks = Trade.find(:all, :conditions=>["bidask_flag=? AND game_id=? AND t_status=? AND player_id=?", "A", game.id, "Openned", player.id])
    %>
    <table width="95%" style="border: 3px solid black; font-size: 18px;">
      <tr align="center" style="border: 3px solid black;" bgcolor="#cccccc">
        <th>Price</th><th>Credit</th><th>Quantity</th>
        <%if game.game_setting.direct_trade=='Y'%>
          <th>Offer To</th>
        <%end%>
        <th></th>
      </tr>
      <%asks.each do |a|%>
      <tr align="center">
        <td><%=a.price%></td>
        <td><%=a.credit_term%></td>
        <td><%=number_with_delimiter(a.quantity)%></td>
        <%if game.game_setting.direct_trade=='Y'%>
          <td>
            <%if a.direct_player_id%>
              <%=Player.find(a.direct_player_id).name%>
            <%else%>
              Market
            <%end%>
          </td>
        <%end%>
        <td><a href="/user/remove_bidoffer/<%=a.id%>">X</a></td>
      </tr>
      <%end%>
    </table>
  </td>
  <td>
    <h3>Traded</h3>
    <table width="95%" style="border: 3px solid black; font-size: 18px;">
      <tr align="center" style="border: 3px solid black;" bgcolor="#cccccc">
        <th>Debtor</th><th>Price</th><th>Credit</th><th>Quantity</th><th>Total</th>
      </tr>
      <%
        sum_quantity = 0
        sum_amount = 0
      %>
      <%ars.each do |ar|%>
      <%
        sum_quantity += ar.quantity
        sum_amount += ar.amount
      %>
      <tr align="center">
        <%if ar.debtor_id==0%>
          <td align="left">MARKET</td>
        <%else%>
          <td align="left"><%=Player.find(ar.debtor_id).name%></td>
        <%end%>
        <td><%=ar.price%></td>
        <td><%=ar.credit_term%></td>
        <td><%=number_with_delimiter(ar.quantity)%></td>
        <td><%=number_with_delimiter(ar.amount)%></td>
      </tr>
      <%end%>
      <tr align="center">
        <td colspan="3">Total</td>
        <td><%=number_with_delimiter(sum_quantity)%></td>
        <td><%=number_with_delimiter(sum_amount)%></td>
      </tr>
    </table>
  </td>
</tr>
</table>